package com.sxfq.loansupermarket.backsystem.modular.analysis.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.analysis.dao.CountTwoDao;
import com.sxfq.loansupermarket.backsystem.modular.analysis.entity.CustomPlatformCountInfo;
import com.sxfq.loansupermarket.backsystem.modular.analysis.entity.PageHomeCount;
import com.sxfq.loansupermarket.backsystem.modular.analysis.entity.PlatFormCount;
import com.sxfq.loansupermarket.backsystem.modular.analysis.entity.RegisterCount;
import com.sxfq.loansupermarket.backsystem.modular.analysis.service.ICountServiceTwo;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gaofucheng:
 * @version 2017年8月24日 上午11:09:22 类说明
 */
@Service
public class CountServiceTwoImpl implements ICountServiceTwo {

	@Autowired
	private SqlMapper sqlMapper;

	@Autowired
	private CountTwoDao countDao;
	private Logger log = Logger.getLogger(this.getClass());
	/*
	 * 数据统计页面的数据列表 即app首页面统计所需要的数据集合
	 */
	@Override
	public List<PageHomeCount> selectHomePageCount(String startTime, String endTime) {
		StringBuffer sql = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sql.append(
				"select home_page_id,button_id,banner_id,star_platform_id,card_page_id,sum(visit_count) as summ from customer_platform_count_info where 1=1");
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			sql.append(" and update_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + endTime
					+ ", 23:59:59') ");
		}
		/*if(StringUtil.isEmpty(startTime) && StringUtil.isEmpty(endTime)){
			Date now = new Date();
			startTime = sdf.format(now);
			sql.append(" and update_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + startTime
					+ ", 23:59:59') ");
		}*/

		sql.append(" group by home_page_id,button_id,banner_id,star_platform_id,card_page_id");
		List<PageHomeCount> buttonList = sqlMapper.selectList(sql.toString(), PageHomeCount.class);
		return buttonList;
	}

	/*
	 * 数据统计页面的数据列表 即app首页面统计登录用户所需要的数据集合
	 */
	@Override
	public List<PageHomeCount> selectHomePageLoginCount(String startTime, String endTime) {
		StringBuffer sql = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sql.append(
				"select home_page_id,button_id,banner_id,star_platform_id,card_page_id,sum(login_visit_count) as summ from customer_platform_count_info where 1=1");
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			sql.append(" and login_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + endTime
					+ ", 23:59:59') ");
		}
		/*if(StringUtil.isEmpty(startTime) && StringUtil.isEmpty(endTime)){
			Date now = new Date();
			startTime = sdf.format(now);
			sql.append(" and update_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + startTime
					+ ", 23:59:59') ");
		}*/
		sql.append(" group by home_page_id,button_id,banner_id,star_platform_id,card_page_id");
		List<PageHomeCount> buttonList = sqlMapper.selectList(sql.toString(), PageHomeCount.class);
		return buttonList;
	}

	/*
	 * 贷款总浏览量
	 */
	@Override
	public Integer getTotalLoanCount(String startTime, String endTime) {
		String sql = " select platform_id as abbreviation,sum(visit_count) as visitCount from customer_platform_count_info where 1=1";
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			sql += " and update_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + endTime
					+ ", 23:59:59') ";
		}
		sql += " group by platform_id ";
		List<PlatFormCount> platformList = sqlMapper.selectList(sql, PlatFormCount.class);
		Integer totalPlatform = 0;
		for (PlatFormCount pfc : platformList) {
			if (StringUtil.isNotEmpty(pfc)) {
				String name = pfc.getAbbreviation();
				if (StringUtil.isNotEmpty(name)) {
					if (pfc.getVisitCount() != null) {
						totalPlatform += pfc.getVisitCount();
					} else {
						totalPlatform += 0;
					}
				}
			}
		}
		return totalPlatform;
	}

	/*
	 * 产品统计的数据列表
	 */
	@Override
	public List<PlatFormCount> selectPlatformCount(String startTime, String endTime) {
		StringBuffer sql = new StringBuffer();
		sql.append(
				"select sum(visit_count) as visitCount,sum(click_count) as clickCount,pp.product_name as abbreviation from customer_platform_count_info cp left join "
						+ "platform_product_info pp on cp.platform_id= pp.id where 1=1");
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			sql.append(" and cp.update_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + endTime
					+ ", 23:59:59') ");
		}
		sql.append(" group by cp.platform_id");
		List<PlatFormCount> pfcList = sqlMapper.selectList(sql.toString(), PlatFormCount.class);
		return pfcList;
	}

	/*
	 * 页码的产品统计数据列表
	 */
	@Override
	public List<PlatFormCount> selectPageListByQueryGetMap(Page<PlatFormCount> page, String beginTime, String endTime) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("page", page);
		if (StringUtil.isNotEmpty(beginTime)) {
			params.put("beginTime", beginTime);
		}
		if (StringUtil.isNotEmpty(endTime)) {
			params.put("endTime", endTime);
		}
		return countDao.getQueryPage(page, params);

	}

	/*
	 * 注册总数
	 */
	@Override
	public Integer getTotalRegister(String startTime, String endTime) {
		String sql = "select count(1) from register_table where 1=1";
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			sql += " and update_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + endTime
					+ ", 23:59:59') ";
		}
		Integer totalRegister = sqlMapper.selectOne(sql, Integer.class);
		return totalRegister;
	}

	/*
	 * 注册统计的数据列表
	 */
	@Override
	public List<RegisterCount> selectRegisterCount(String startTime, String endTime) {
		StringBuffer sql = new StringBuffer();
		sql.append(
				"select count(1) as registerCount, channel_name,click_count from register_table rt left join cms_channel cc on cc.id= rt.channel_id "
						+ "left join customer_platform_count_info cp on cp.channel_id = cc.id where 1=1");
		if (StringUtil.isNotEmpty(startTime) && StringUtil.isNotEmpty(endTime)) {
			sql.append(" and cp.update_time between CONCAT('" + startTime + ", 00:00:00') and CONCAT('" + endTime
					+ ", 23:59:59') ");
		}
		sql.append(" group by channel_id");
		List<RegisterCount> registerList = sqlMapper.selectList(sql.toString(), RegisterCount.class);
		return registerList;
	}

	/*
	 * 获取首页banner个数
	 */
	@Override
	public Integer getBannerNum() {
		String sql = "select count(1) from cms_content where channel_id=2 and status=1";
		Integer bannerNum = sqlMapper.selectOne(sql, Integer.class);
		return bannerNum;
	}

	/*
     * 查询是id是否存在，是否是新的一天的开始
     */
	@Override
	public boolean isExist(String platformId,String buttonId,String starPlatformId,String bannerId,String homePageId, Date date) {
		CustomPlatformCountInfo cpc = selectCustomPlatformCountInfo(platformId,buttonId,starPlatformId,bannerId,homePageId,date);
		boolean b = false;
		if (cpc.getNum() != 0){
			b = true;//更新
		}
		log.info("cpc.getNum:"+cpc.getNum());


		return b;//新增
	}

	/*
     * 查询是id是否存在，是否是新的一天的开始
     */
	@Override
	public boolean isExistCardPage(String platformId,String buttonId,String starPlatformId,String bannerId,String cardPageId, Date date) {
		CustomPlatformCountInfo cpc = selectCustomCardPageCountInfo(platformId,buttonId,starPlatformId,bannerId,cardPageId,date);
		boolean b = false;
		if (cpc.getNum() != 0){
			b = true;//更新
		}
		log.info("cpc.getNum:"+cpc.getNum());


		return b;//新增
	}

	/*
     * 查询是id是否存在，是否是新的一天的开始
     */
	@Override
	public boolean isExistLogin(String platformId,String buttonId,String starPlatformId,String bannerId,String homePageId, Date date,String phone) {
		int size = selectLoginCustomPlatformCountInfo(platformId,buttonId,starPlatformId,bannerId,homePageId,date,phone);
		boolean b = false;
		if (size > 0){
			b = true;//更新
		}
//        log.info("cpc.getNum:"+cpc.getNum());


		return b;//新增
	}

	/*
     * 查询是id是否存在，是否是新的一天的开始
     */
	@Override
	public boolean isExistRegister(String platformId,Date date,String phone) {
		int size = selectRegisterCustomPlatformCountInfo(platformId,date,phone);
		boolean b = false;
		if (size > 0){
			b = true;//更新
		}
//        log.info("cpc.getNum:"+cpc.getNum());


		return b;//新增
	}

	/*
     * 查询是id是否存在，是否是新的一天的开始
     */
	@Override
	public boolean isExistChannel(String channel) {
		int size = 0;
		String sql = "select count(1) num from bw_order_channel where  id ='"+channel+"'";
		size = sqlMapper.selectOne(sql,Integer.class);
		boolean b = false;
		if (size > 0){
			b = true;//存在该channel
		}
//        log.info("cpc.getNum:"+cpc.getNum());


		return b;//不存在channel
	}

	/*
     * 查询是id是否存在，是否是新的一天的开始
     */
	@Override
	public boolean isExistCardPage(String platformId,String buttonId,String starPlatformId,String bannerId,String homePageId,String cardPageId,Date date,String phone) {
		int size = selectLoginCustomPlatformCountInfo(platformId,buttonId,starPlatformId,bannerId,homePageId,cardPageId,date,phone);
		boolean b = false;
		if (size > 0){
			b = true;//更新
		}
//        log.info("cpc.getNum:"+cpc.getNum());


		return b;//新增
	}




	/*
     * 根据条件查询表
     * date表示是当天日期
     */
	@Override
	public CustomPlatformCountInfo selectCustomPlatformCountInfo(String platformId,String buttonId,String starPlatformId,String bannerId,String homePageId, Date date){

		//Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String updateDay=sdf.format(date);
		StringBuffer sql=new StringBuffer();
		sql.append("select count(1) num, id, platform_id platformId,home_page_id homePageId ,star_platform_id stratPlatfromId,banner_id bannerId,button_id buttonId,visit_count visitCount,click_count clickCount,login_visit_count loginVisitCount,login_click_count loginClickCount from customer_platform_count_info  " +
				"where 1=1 and update_time between CONCAT('" + updateDay + ", 00:00:00') and CONCAT('" + updateDay
				+ ", 23:59:59')");
//        sql.append("select count(1) num from customer_platform_count_info  " +
//                "where 1=1 and update_time between CONCAT('" + updateDay + ", 00:00:00') and CONCAT('" + updateDay
//                + ", 23:59:59')");
		if(StringUtil.isNotEmpty(platformId)){
			sql.append(" and platform_id="+platformId);
			log.info("platformId:"+platformId);
		}
		if(StringUtil.isNotEmpty(starPlatformId)){
			sql.append(" and star_platform_id="+starPlatformId);
		}
		if(StringUtil.isNotEmpty(bannerId)){
			sql.append(" and banner_id="+bannerId);
		}
		if(StringUtil.isNotEmpty(buttonId)){
			sql.append(" and button_id="+buttonId);
		}
		if(StringUtil.isNotEmpty(homePageId)){
			sql.append(" and home_page_id="+homePageId);
			log.info("homePageId："+homePageId);
		}

		String str=sql.toString();
		CustomPlatformCountInfo cpc = sqlMapper.selectOne(str,CustomPlatformCountInfo.class);

		log.info("--------"+cpc+"   cpc.getVisitCount: "+cpc.getVisitCount());
		return cpc;
	}

	/*
     * 根据条件查询表
     * date表示是当天日期
     */
	public CustomPlatformCountInfo selectCustomCardPageCountInfo(String platformId,String buttonId,String starPlatformId,String bannerId,String cardPageId, Date date){

		//Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String updateDay=sdf.format(date);
		StringBuffer sql=new StringBuffer();
		sql.append("select count(1) num from customer_platform_count_info  " +
				"where 1=1 and update_time between CONCAT('" + updateDay + ", 00:00:00') and CONCAT('" + updateDay
				+ ", 23:59:59')");
		if(StringUtil.isNotEmpty(platformId)){
			sql.append(" and platform_id="+platformId);
			log.info("platformId:"+platformId);
		}
		if(StringUtil.isNotEmpty(starPlatformId)){
			sql.append(" and star_platform_id="+starPlatformId);
		}
		if(StringUtil.isNotEmpty(bannerId)){
			sql.append(" and banner_id="+bannerId);
		}
		if(StringUtil.isNotEmpty(buttonId)){
			sql.append(" and button_id="+buttonId);
		}
		if(StringUtil.isNotEmpty(cardPageId)){
			sql.append(" and card_page_id="+cardPageId);
			log.info("card_page_id："+cardPageId);
		}

		String str=sql.toString();
		CustomPlatformCountInfo cpc = sqlMapper.selectOne(str,CustomPlatformCountInfo.class);
		log.info("--------"+cpc+"   cpc.getVisitCount: "+cpc.getVisitCount());
		return cpc;
	}

	/*
     * 根据条件查询表
     * date表示是当天日期
     */
	public int selectLoginCustomPlatformCountInfo(String platformId,String buttonId,String starPlatformId,String bannerId,String homePageId, Date date,String phone){

		//Date date=new Date();
		int size = 0;
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String updateDay=sdf.format(date);
		StringBuffer sql=new StringBuffer();
		sql.append("select count(1) num from customer_platform_count_info  " +
				"where 1=1 ");
		if(StringUtil.isNotEmpty(platformId)){
			sql.append(" and platform_id="+platformId);
			log.info("platformId:"+platformId);
		}
		if(StringUtil.isNotEmpty(starPlatformId)){
			sql.append(" and star_platform_id="+starPlatformId);
		}
		if(StringUtil.isNotEmpty(bannerId)){
			sql.append(" and banner_id="+bannerId);
		}
		if(StringUtil.isNotEmpty(buttonId)){
			sql.append(" and button_id="+buttonId);
		}
		if(StringUtil.isNotEmpty(homePageId)){
			sql.append(" and home_page_id="+homePageId);
			log.info("homePageId："+homePageId);
		}
		if(StringUtil.isNotEmpty(phone)){
			sql.append(" and phone="+phone);
			log.info("phone："+phone);
		}
		if(StringUtil.isNotEmpty(updateDay)){
			sql.append(" and login_time='"+updateDay+" 00:00:00'");
			log.info("login_time："+updateDay);
		}

		String str=sql.toString();
//        CustomPlatformCountInfo cpc = sqlMapper.selectOne(str,CustomPlatformCountInfo.class);
//        List<Map<String, Object>> list =  sqlMapper.selectList(str);
		size = sqlMapper.selectOne(str,Integer.class);
//        log.info("--------"+size+"   cpc.getVisitCount: "+cpc.getVisitCount());
		return size;
	}

	/*
     * 根据条件查询表
     * date表示是当天日期
     */
	public int selectRegisterCustomPlatformCountInfo(String platformId, Date date,String phone){

		//Date date=new Date();
		int size = 0;
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String updateDay=sdf.format(date);
		StringBuffer sql=new StringBuffer();
		sql.append("select count(1) num from customer_platform_count_info  " +
				"where 1=1 ");
		if(StringUtil.isNotEmpty(platformId)){
			sql.append(" and platform_id="+platformId);
			log.info("platformId:"+platformId);
		}
		if(StringUtil.isNotEmpty(phone)){
			sql.append(" and phone="+phone);
			log.info("phone："+phone);
		}
		if(StringUtil.isNotEmpty(updateDay)){
			sql.append(" and regis_time='"+updateDay+" 00:00:00'");
			log.info("regis_time："+updateDay);
		}


		String str=sql.toString();
//        CustomPlatformCountInfo cpc = sqlMapper.selectOne(str,CustomPlatformCountInfo.class);
//        log.info("--------"+cpc+"   cpc.getLoginClickCount: "+cpc.getLoginClickCount());
//        List<Map<String, Object>> list =  sqlMapper.selectList(str);
		size = size = sqlMapper.selectOne(str,Integer.class);
		return size;
	}

	/*
     * 根据条件查询表
     * date表示是当天日期
     */
	public int selectLoginCustomPlatformCountInfo(String platformId,String buttonId,String starPlatformId,String bannerId,String homePageId,String cardPageId, Date date,String phone){

		//Date date=new Date();
		int size = 0;
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		String updateDay=sdf.format(date);
		StringBuffer sql=new StringBuffer();
		sql.append("select count(1) num from customer_platform_count_info  " +
				"where 1=1 ");
		if(StringUtil.isNotEmpty(platformId)){
			sql.append(" and platform_id="+platformId);
			log.info("platformId:"+platformId);
		}
		if(StringUtil.isNotEmpty(starPlatformId)){
			sql.append(" and star_platform_id="+starPlatformId);
		}
		if(StringUtil.isNotEmpty(bannerId)){
			sql.append(" and banner_id="+bannerId);
		}
		if(StringUtil.isNotEmpty(buttonId)){
			sql.append(" and button_id="+buttonId);
		}
		if(StringUtil.isNotEmpty(homePageId)){
			sql.append(" and home_page_id="+homePageId);
			log.info("homePageId："+homePageId);
		}
		if(StringUtil.isNotEmpty(cardPageId)){
			sql.append(" and card_page_id="+cardPageId);
			log.info("cardPageId："+cardPageId);
		}
		if(StringUtil.isNotEmpty(phone)){
			sql.append(" and phone="+phone);
			log.info("phone："+phone);
		}
		if(StringUtil.isNotEmpty(updateDay)){
			sql.append(" and login_time='"+updateDay+" 00:00:00'");
			log.info("login_time："+updateDay);
		}

		String str=sql.toString();
//        CustomPlatformCountInfo cpc = sqlMapper.selectOne(str,CustomPlatformCountInfo.class);
//        log.info("--------"+cpc+"   cpc.getVisitCount: "+cpc.getVisitCount());
//        List<Map<String, Object>> list =  sqlMapper.selectList(str);
		size = size = sqlMapper.selectOne(str,Integer.class);
		return size;
	}


	/*
     * 插入新的一天的第一条数据
     */
	@Override
	public int insertCustomPlatformCountInfo(String platformId,String buttonId,String starPlatformId,String bannerId,String cardPageId, Date date,Integer stuts) {
		SimpleDateFormat sdfCreate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
		SimpleDateFormat sdfUpdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String creatDay=sdfCreate.format(date);//创建时间
		String sql = null;
		log.info("创建时间："+creatDay);
		String updateDay=sdfUpdate.format(date);//修改时间
		log.info("创建时间："+updateDay);
		int id = 0;
		try {
			if (stuts == 0){
				sql="insert into customer_platform_count_info ";
				if (StringUtil.isNotEmpty(platformId)){
					sql += "(platform_id,visit_count,creat_time,update_time) values ('"+platformId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"') ";
				}
				if (StringUtil.isNotEmpty(buttonId)){
					sql += "(button_id,visit_count,creat_time,update_time) values ('"+buttonId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"') ";
				}
				if (StringUtil.isNotEmpty(starPlatformId)){
					sql += "(star_platform_id,visit_count,creat_time,update_time) values ('"+starPlatformId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"') ";
				}
				if (StringUtil.isNotEmpty(bannerId)){
					sql += "(banner_id,visit_count,creat_time,update_time) values ('"+bannerId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"') ";
				}
				if (StringUtil.isNotEmpty(cardPageId)){
					sql += "(card_page_id,visit_count,creat_time,update_time) values ('"+cardPageId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"') ";
				}
				id = sqlMapper.insert(sql);
			}else {
				sql="insert into customer_platform_count_info ";
				if (StringUtil.isNotEmpty(platformId)){
					sql += "(card_page_id,click_count,creat_time,update_time) values ('"+cardPageId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"') ";
				}
				id = sqlMapper.insert(sql);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("插入数据异常", e);
		}
		return id;
	}

	/*
     * 插入登录用户新的一天的第一条数据
     */
	@Override
	public int insertLoginCustomPlatformCountInfo(String platformId,String buttonId,String starPlatformId,String bannerId,String homePageId, Date date,Integer stuts,String phone) {
		SimpleDateFormat sdfCreate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
		SimpleDateFormat sdfUpdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdfLogin = new SimpleDateFormat("yyyy-MM-dd");
		String creatDay=sdfCreate.format(date);//创建时间
		String sql = null;
		log.info("创建时间："+creatDay);
		String updateDay=sdfUpdate.format(date);//修改时间
		log.info("创建时间："+updateDay);
		String loginTime = sdfLogin.format(date);
		int id = 0;
		try {
			if (stuts == 0){
				sql="insert into customer_platform_count_info ";
				if (StringUtil.isNotEmpty(platformId)){
					sql += "(platform_id,login_visit_count,creat_time,update_time,phone,login_time) values ('"+platformId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"','"+phone+"','"+loginTime+"') ";
				}
				if (StringUtil.isNotEmpty(buttonId)){
					sql += "(button_id,login_visit_count,creat_time,update_time,phone,login_time) values ('"+buttonId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"','"+phone+"','"+loginTime+"') ";
				}
				if (StringUtil.isNotEmpty(starPlatformId)){
					sql += "(star_platform_id,login_visit_count,creat_time,update_time,phone,login_time) values ('"+starPlatformId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"','"+phone+"','"+loginTime+"') ";
				}
				if (StringUtil.isNotEmpty(bannerId)){
					sql += "(banner_id,login_visit_count,creat_time,update_time,phone,login_time) values ('"+bannerId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"','"+phone+"','"+loginTime+"') ";
				}
				if (StringUtil.isNotEmpty(homePageId)){
					sql += "(home_page_id,login_visit_count,creat_time,update_time,phone,login_time) values ('"+homePageId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"','"+phone+"','"+loginTime+"') ";
				}
				id = sqlMapper.insert(sql);
			}else {
				sql="insert into customer_platform_count_info ";
				if (StringUtil.isNotEmpty(platformId)){
					sql += "(platform_id,login_click_count,creat_time,update_time,phone,login_time) values ('"+platformId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"','"+phone+"','"+loginTime+"') ";
				}
				id = sqlMapper.insert(sql);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("插入数据异常", e);
		}
		return id;
	}

	/*
     * 插入登录用户新的一天的第一条数据
     */
	@Override
	public int insertRegisterCustomPlatformCountInfo(String platformId,Date date,Integer stuts,String phone,Integer channel) {
		SimpleDateFormat sdfCreate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
		SimpleDateFormat sdfUpdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdfLogin = new SimpleDateFormat("yyyy-MM-dd");
		String creatDay=sdfCreate.format(date);//创建时间
		String sql = null;
		log.info("创建时间："+creatDay);
		String updateDay=sdfUpdate.format(date);//修改时间
		log.info("创建时间："+updateDay);
		String regisTime = sdfLogin.format(date);
		int id = 0;
		try {
			if (stuts == 0){
				sql="insert into customer_platform_count_info ";
				if (StringUtil.isNotEmpty(platformId)){
					sql += "(platform_id,login_visit_count,creat_time,update_time,phone,regis_time) values ('"+platformId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"','"+phone+"','"+regisTime+"') ";
				}

				id = sqlMapper.insert(sql);
			}else {
				sql="insert into customer_platform_count_info ";
				if (StringUtil.isNotEmpty(platformId)){
					sql += "(platform_id,login_click_count,creat_time,update_time,phone,regis_time,channel_id) values ('"+platformId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"','"+phone+"','"+regisTime+"','"+channel+"') ";
				}
				id = sqlMapper.insert(sql);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("插入数据异常", e);
		}
		return id;
	}

	/*
     * 插入登录用户新的一天的第一条数据
     */
	@Override
	public int insertLoginCustomCardCountInfo(String platformId,String buttonId,String starPlatformId,String bannerId,String homePageId,String cardPageId, Date date,Integer stuts,String phone) {
		SimpleDateFormat sdfCreate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
		SimpleDateFormat sdfUpdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sdfLogin = new SimpleDateFormat("yyyy-MM-dd");
		String creatDay=sdfCreate.format(date);//创建时间
		String sql = null;
		log.info("创建时间："+creatDay);
		String updateDay=sdfUpdate.format(date);//修改时间
		log.info("创建时间："+updateDay);
		String loginTime = sdfLogin.format(date);
		int id = 0;
		try {
			if (stuts == 0){
				sql="insert into customer_platform_count_info ";
				if (StringUtil.isNotEmpty(platformId)){
					sql += "(platform_id,login_visit_count,creat_time,update_time,phone,login_time) values ('"+platformId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"') ";
				}
				if (StringUtil.isNotEmpty(buttonId)){
					sql += "(button_id,login_visit_count,creat_time,update_time,phone,login_time) values ('"+buttonId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"','"+phone+"','"+loginTime+"') ";
				}
				if (StringUtil.isNotEmpty(starPlatformId)){
					sql += "(star_platform_id,login_visit_count,creat_time,update_time,phone,login_time) values ('"+starPlatformId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"','"+phone+"','"+loginTime+"') ";
				}
				if (StringUtil.isNotEmpty(bannerId)){
					sql += "(banner_id,login_visit_count,creat_time,update_time,phone,login_time) values ('"+bannerId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"','"+phone+"','"+loginTime+"') ";
				}
				if (StringUtil.isNotEmpty(homePageId)){
					sql += "(home_page_id,login_visit_count,creat_time,update_time,phone,login_time) values ('"+homePageId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"','"+phone+"','"+loginTime+"') ";
				}
				if (StringUtil.isNotEmpty(cardPageId)){
					sql += "(card_page_id,login_visit_count,creat_time,update_time,phone,login_time) values ('"+cardPageId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"','"+phone+"','"+loginTime+"') ";
				}
				id = sqlMapper.insert(sql);
			}else {
				sql="insert into customer_platform_count_info ";
				if (StringUtil.isNotEmpty(platformId)){
					sql += "(card_page_id,login_click_count,creat_time,update_time,phone,login_time) values ('"+cardPageId+"'"+",'"+1+"'"+",'"+
							creatDay+"','"+updateDay+"','"+phone+"','"+loginTime+"') ";
				}
				id = sqlMapper.insert(sql);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("插入数据异常", e);
		}
		return id;
	}
	/*
     * 更新访问量
     */
	@Override
	public int updateCount(String platformId,String buttonId,String starPlatformId,String bannerId,String homePageId ,Date date,Integer stuts) {
		CustomPlatformCountInfo cpc = selectCustomPlatformCountInfo(platformId,buttonId,starPlatformId,bannerId,homePageId,date);
		long visit = 0;
		String sql = null;
		int updataCount = 0;
		if (stuts == 0){
			log.info("visitCount:"+cpc.getVisitCount());
			if (cpc.getVisitCount() == null){
				visit = 1;
			}else {
				visit = cpc.getVisitCount() + 1;
			}
			sql = ("update customer_platform_count_info set visit_count=" + visit + " where id=" + cpc.getId());
			updataCount = sqlMapper.update(sql);
		}else {
			if (cpc.getClickCount() == null){
				visit = 1;
			}else {
				visit = cpc.getClickCount() + 1;
			}
			sql =("update customer_platform_count_info set click_Count="+visit+" where id="+cpc.getId());
			updataCount=sqlMapper.update(sql);
		}

		return updataCount;
	}

	/*
     * 更新访问量
     */
	@Override
	public int updateCardPageCount(String platformId,String buttonId,String starPlatformId,String bannerId,String cardPageId ,Date date,Integer stuts) {
		CustomPlatformCountInfo cpc = selectCustomCardPageCountInfo(platformId,buttonId,starPlatformId,bannerId,cardPageId,date);
		long visit = 0;
		String sql = null;
		int updataCount = 0;
		if (stuts == 0){
			log.info("visitCount:"+cpc.getVisitCount());
			if (cpc.getVisitCount() == null){
				visit = 1;
			}else {
				visit = cpc.getVisitCount() + 1;
			}
			sql = ("update customer_platform_count_info set visit_count=" + visit + " where id=" + cpc.getId());
			updataCount = sqlMapper.update(sql);
		}else {
			if (cpc.getClickCount() == null){
				visit = 1;
			}else {
				visit = cpc.getClickCount() + 1;
			}
			sql =("update customer_platform_count_info set click_Count="+visit+" where id="+cpc.getId());
			updataCount=sqlMapper.update(sql);
		}

		return updataCount;
	}

	/*
     * 更新登录用户访问量
     */
	@Override
	public int updateLoginCount(String platformId,String buttonId,String starPlatformId,String bannerId,String homePageId ,Date date,Integer stuts) {
		CustomPlatformCountInfo cpc = selectCustomPlatformCountInfo(platformId,buttonId,starPlatformId,bannerId,homePageId,date);
		long visit = 0;
		String sql = null;
		int updataCount = 0;
		if (stuts == 0){
			log.info("loginVisitCount:"+cpc.getLoginVisitCount());
			if (cpc.getLoginVisitCount() == null){
				visit = 1;
			}else {
				visit = cpc.getLoginVisitCount() + 1;
			}
			sql = ("update customer_platform_count_info set login_visit_count=" + visit + " where id=" + cpc.getId());
			updataCount = sqlMapper.update(sql);
		}else {
			if (cpc.getLoginClickCount() == null){
				visit = 1;
			}else {
				visit = cpc.getLoginClickCount() + 1;
			}
			sql =("update customer_platform_count_info set login_click_count="+visit+" where id="+cpc.getId());
			updataCount=sqlMapper.update(sql);
		}

		return updataCount;
	}

}
